using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Configuration;

using SBPweb.Themes.Windows.Skins;
using SBPweb.Themes.Windows;

namespace SBPweb.Themes.Windows
{
	/// <summary>
	/// Class of DefaultLookAndFeel
	/// </summary>
	public class DefaultLookAndFeel: Component
	{
		#region Private members
		private static Dictionary<string, Skin> mAvailableSkins = new Dictionary<string, Skin>();
		private static Skin mSkin;
		#endregion

		#region Static properties
		/// <summary>
		/// Constructor of DefaultLookAndFeel
		/// </summary>
		static DefaultLookAndFeel()
		{
			InitializeSkins();
		}

		/// <summary>
		/// List of available skins
		/// </summary>
		public static Dictionary<string, Skin> AvailableSkins
		{
			get
			{
				return mAvailableSkins;
			}
		}

		/// <summary>
		/// Current skin
		/// </summary>
		public static Skin Skin
		{
			get
			{
				return mSkin;
			}
			set
			{
				mSkin = value;
				if (OnSkinChanged != null)
				{
					OnSkinChanged.Invoke(value, new EventArgs());
				}
			}
		}
		#endregion

		#region Public properties

		/// <summary>
		/// Default skin
		/// </summary>
		[Category("LookAndFeel")]
		public Skin DefaultSkin
		{
			get
			{
				return Skin;
			}
			set
			{
				Skin = value;
			}
		}

		#endregion

		#region Events

		/// <summary>
		/// Raised when the current skin changed
		/// </summary>
		public static event EventHandler OnSkinChanged;

		#endregion

		#region Private helper methods

		private static void InitializeSkins()
		{
			Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
			SkinConfigurationSection scs = conf.GetSection("skinConfiguration") as SkinConfigurationSection;

			if (scs == null || scs.AvailableSkins == null)
			{
				return;
			}

			foreach(string key in scs.AvailableSkins.AllKeys)
			{
				KeyValueConfigurationElement kvce = scs.AvailableSkins[key];
				Assembly asm = Assembly.Load(kvce.Value);
				if (!Skin.IsSkinPackage(asm))
				{
					mAvailableSkins.Add(kvce.Key, new Skin(asm));
				}
				else
				{
					//mAvailableSkins.AddRange(Skin.ExtractSkinsFromPackage(asm));
				}
			}
		}

		#endregion
	}
}
